package tx.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class BookDaoImpl implements BookDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //查询图书价格
    @Override
    public int getBookPrice(int bookId) {
        String sql = "select price from t_book where bookId = ?";
        Integer price = jdbcTemplate.queryForObject(sql, Integer.class, bookId);
        return price;
    }

    //更新图书库存
    @Override
    public void updateStock(int bookId) {
        String sql = "update t_book set stock = stock-1 where bookId = ?";
        jdbcTemplate.update(sql, bookId);
    }

    //更新用户余额
    @Override
    public void updateUserBalance(int userId, int price) {
        String sql = "update t_book set balance = balance-? where userId = ?";
        jdbcTemplate.update(sql, price, userId);
    }
}
